package org.demo.java2d;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * http://www.ibm.com/developerworks/cn/java/j-synth/
 * http://hi.baidu.com/laryii/blog/item/de6a84d0d431ec299b5027a4.html
 * @author  
 * @date    2010-5-12
 * @file    org.demo.java2d.Java2DButton.java
 */
public class Java2DButton {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		int width = 120;
		int height = 20;
		String filePath = "D:\\Temp\\tmp.png";
		float borderWidth = 1.0f;
		Color borderColor = new Color(131, 226, 226);
		Color startColor = Color.decode("0xCDEB8B");
		Color endColor = Color.decode("0x008C00");
		
		// 
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = image.createGraphics();
		image = g.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
		g.dispose();		
		g = image.createGraphics();
		
		// file color
		GradientPaint paint = new GradientPaint(0, 0, startColor, 0, height/2, endColor);
		g.setPaint(paint);
		g.fillRoundRect(0, 0, 120, 20, 10, 10);
		
		// draw border
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		RoundRectangle2D rect = new RoundRectangle2D.Double(0, 0, 119, 19, 8, 8);
		g.setColor(borderColor);
		g.setStroke(new BasicStroke(borderWidth));
		g.draw(rect);
		
		// save to file
		g.dispose();
		ImageIO.write(image, "png", new File(filePath));
	}		
}
